package com.atos.optical.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.atos.optical.Enums.StaticEnums;
import com.atos.optical.common.BaseServiceImpl;
import com.atos.optical.common.ServiceException;
import com.atos.optical.mapper.SysMenuInfoMapper;
import com.atos.optical.mapper.SysUserMenuRelMapper;
import com.atos.optical.model.bo.TreeNodeBo;
import com.atos.optical.model.po.SysMenuInfo;
import com.atos.optical.service.SysMenuInfoService;
import com.atos.optical.utils.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wanghra
 * @version 1.0
 * @description
 * @date 2025/8/13
 */
@Slf4j
@Service
public class SysMenuInfoServiceImpl extends BaseServiceImpl<SysMenuInfoMapper,
        SysMenuInfo> implements SysMenuInfoService {
    @Autowired
    private SysMenuInfoMapper mapper;
    @Autowired
    private SysUserMenuRelMapper userMenuRelMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(SysMenuInfo entity) {
        check(entity);
        return super.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(SysMenuInfo entity) {
        check(entity);
        return super.updateById(entity);
    }

    @Override
    public List<TreeNodeBo> getAllTree() {
        QueryWrapper<SysMenuInfo> query = new QueryWrapper<>();
        query.lambda().orderByAsc(SysMenuInfo::getMenuSort, SysMenuInfo::getCreateTime);
        List<SysMenuInfo> list = mapper.selectList(query);
        return TreeUtil.listToTree(list, StaticEnums.TREE_ROOT);
    }

    @Override
    public List<TreeNodeBo> getUserMenuTree(String userId) {
        List<TreeNodeBo> list = new ArrayList<>();
        List<SysMenuInfo> menuInfoList = userMenuRelMapper.selectUserMenuTree(userId);
        if (CollUtil.isNotEmpty(menuInfoList)) {
            list = TreeUtil.listToTree(menuInfoList, StaticEnums.TREE_ROOT);
        }
        return list;
    }

    private void check(SysMenuInfo entity) {
        if (isExistName(entity)) {
            throw new ServiceException("菜单名称重复！");
        }
        if (isExistRouteName(entity)) {
            throw new ServiceException("路由名称重复！");
        }
    }

    private boolean isExistName(SysMenuInfo entity) {
        QueryWrapper<SysMenuInfo> query = new QueryWrapper<>();
        query.lambda().ne(StrUtil.isNotBlank(entity.getMenuId()), SysMenuInfo::getMenuId, entity.getMenuId())
                .eq(SysMenuInfo::getMenuName, entity.getMenuName());
        return 0 < mapper.selectCount(query);
    }

    private boolean isExistRouteName(SysMenuInfo entity) {
        QueryWrapper<SysMenuInfo> query = new QueryWrapper<>();
        query.lambda().ne(StrUtil.isNotBlank(entity.getMenuId()), SysMenuInfo::getMenuId, entity.getMenuId())
                .eq(SysMenuInfo::getMenuRouteName, entity.getMenuRouteName());
        return 0 < mapper.selectCount(query);
    }
}
